#include "Octree.h"
#include "gameworld.h"

Octree::Octree(int levels, float width, float height, float depth)
{
	m_levels = levels;
	m_width = width;
	m_height = height;
	m_depth = depth;

	entities = new std::list<Entity*>();
	clumps = new std::list<TerrainClump*>();
	colliders = new std::list<Collider*>();

	float dist = GameWorld::OCTREE_SIZE / 2;
	float rad = sqrt(dist*dist + dist*dist);
	rad = sqrt(rad*rad + dist*dist);
	root = new Node(NULL,levels-1, levels,0,height/2,0,rad);
}


Octree::~Octree(void)
{
	std::list<Entity*>::iterator it;
	for (it=entities->begin() ; it != entities->end(); it++ ){
		delete *it;
	}
	entities->clear();

	delete root;
	root = NULL;
}

void Octree::render(int mode, Frustum& frustum)
{
	root->render(mode, frustum);
}

void Octree::scopeEntities()
{
	root->removeAllEntities();

	std::list<Entity*>::iterator it;
	for (it=entities->begin() ; it != entities->end(); it++ ){
		scopeEntity(*it);
	}
}
void Octree::scopeTerrain()
{
	std::list<TerrainClump*>::iterator it;
	for (it=clumps->begin() ; it != clumps->end(); it++ ){
		root->putInNode(*it);
	}
}

void Octree::scopeEntity(Entity* ent)
{
	root->putInNode(ent);
}